#include "Uart.h"
#include "stm32h7xx_hal.h"

#define RxBufSize                      

UART_HandleTypeDef huart1 = {0};
uint8_t RxBuf[2][RxBufSize] __attribute__((section (".RAM_D1")));
void (*Uart1RxCompleteCallback)(uint8_t *pData,uint16_t *Count);

void Uart1Init(uint32_t BaudRate,void (*RxCompleteCallback)(uint8_t *pData,uint16_t *Count))
{
        huart1.Instance = USART1;
        huart1.Init.BaudRate = BaudRate;
        huart1.Init.ClockPrescaler = UART_PRESCALER_DIV2;
        huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
        huart1.Init.Mode = UART_MODE_TX_RX;
        huart1.Init.OneBitSampling = UART_ONEBIT_SAMPLING_DISABLED;
        huart1.Init.OverSampling = UART_OVERSAMPLING_8;
        huart1.Init.Parity = UART_PARITY_NONE;
        huart1.Init.StopBits = UART_STOPBITS_1;
        huart1.Init.WordLength = UART_WORDLENGTH_8B;
        huart1.FifoMode = UART_FIFOMODE_DISABLE;
        
        HAL_UART_Init(&huart1);
        HAL_UART_Receive_IT(&huart1,RxBuf[0],RxBufSize);
        __HAL_UART_ENABLE_IT(&huart1,UART_IT_IDLE);
        HAL_NVIC_EnableIRQ(USART1_IRQn);
        HAL_NVIC_SetPriority(USART1_IRQn,11,0);
        
        Uart1RxCompleteCallback = RxCompleteCallback;
}

void HAL_UART_MspInit(UART_HandleTypeDef *huart)
{
        if(huart == &huart1)//串口1
        {
                GPIO_InitTypeDef GPIO_InitStruct;
          
                GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
                GPIO_InitStruct.Pull = GPIO_NOPULL;
                GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM;
                GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
                GPIO_InitStruct.Pin = GPIO_PIN_10 | GPIO_PIN_9;
        
                __HAL_RCC_GPIOA_CLK_ENABLE();
                __HAL_RCC_USART1_CLK_ENABLE();
               
                HAL_GPIO_Init(GPIOA,&GPIO_InitStruct);
        }
}

void Uart1TxData(uint8_t *pData,uint16_t Count)
{
        HAL_UART_Transmit_IT(&huart1,pData,Count);
} 

void USART1_IRQHandler(void) 
{
        HAL_UART_IRQHandler(&huart1); 
        if(__HAL_UART_GET_FLAG(&huart1,UART_FLAG_IDLE))
        {
                static uint16_t count;
                __HAL_UART_CLEAR_IDLEFLAG(&huart1);
                if(Uart1RxCompleteCallback)
                {
                        count = RxBufSize - huart1.RxXferCount;
                        huart1.RxState = HAL_UART_STATE_READY;
                        if(huart1.pRxBuffPtr <= RxBuf[1])
                                Uart1RxCompleteCallback(RxBuf[0],&count);
                        else
                                Uart1RxCompleteCallback(RxBuf[1],&count);
                        HAL_UART_RxCpltCallback(&huart1);
                }
        }
}

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
        if(huart == &huart1)
        {
                if(huart->pRxBuffPtr <= RxBuf[1])
                        HAL_UART_Receive_IT(&huart1,RxBuf[1],RxBufSize);
                else
                        HAL_UART_Receive_IT(&huart1,RxBuf[0],RxBufSize);
        }
}